﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AxZWANPHONELib;

namespace TopVisionVideoStream
{
    public static class OcxApi
    {
        private static AxZwanPhone ocx { get => Configuration.Core; }


        #region Login

        /// <summary>
        /// 登录验证事件
        /// </summary>
        public static event _DZwanPhoneEvents_LogonResultEventHandler LogonResultEvent
        {
            add { ocx.LogonResult += value; }
            remove { ocx.LogonResult -= value; }
        }
        /// <summary>
        /// 服务器注册事件
        /// </summary>
        public static event _DZwanPhoneEvents_RegisterSoftPhoneToSSEventHandler RegisterSoftPhoneToSSEvent
        {
            add { ocx.RegisterSoftPhoneToSS += value; }
            remove { ocx.RegisterSoftPhoneToSS -= value; }
        }

        public static event _DZwanPhoneEvents_MessageInfoEventHandler MessageInfoEvent
        {
            add { ocx.MessageInfo += value; }
            remove { ocx.MessageInfo -= value; }
        }


        /// <summary>
        /// 登录服务器
        /// </summary>
        /// <param name="lpUserName">用户名</param>
        /// <param name="lpPassWord">用户密码</param>
        /// <param name="lpBindPhone"></param>
        /// <param name="lpVersion">默认 1.0</param>
        /// <returns></returns>
        public static bool bLogonServer2(string lpUserName, string lpPassWord, string lpBindPhone, string lpVersion = "1.0")
        {
            sbyte ret = ocx.bLogonServer2(lpUserName, lpPassWord, lpBindPhone, lpVersion);
            return ret != 0;
        }

        #endregion


        #region Logout

        /// <summary>
        /// 注销事件
        /// </summary>
        public static event EventHandler LogoutResultEvent
        {
            add { ocx.LogoutServerSuccess += value; }
            remove { ocx.LogoutServerSuccess -= value; }
        }

        /// <summary>
        /// 注销账号
        /// </summary>
        /// <returns></returns>
        public static bool Logout()
        {
            sbyte ret = ocx.bLogoutServer();
            return ret != 0;
        }

        #endregion


        #region VieoCall

        public static event _DZwanPhoneEvents_PhoneConnectedEventHandler ConnectedEvent
        {
            add { ocx.PhoneConnected += value; }
            remove { ocx.PhoneConnected -= value; }
        }
        public static event _DZwanPhoneEvents_ReportMemberStatusEventHandler ReportMemberStatusEvent
        {
            add { ocx.ReportMemberStatus += value; }
            remove { ocx.ReportMemberStatus -= value; }
        }
        public static event _DZwanPhoneEvents_SomeOneCallMeEventHandler SomeOneCallMeEvent
        {
            add { ocx.SomeOneCallMe += value; }
            remove { ocx.SomeOneCallMe -= value; }
        }

        public static bool VideoCallP2P(string callID, int callType = 2)
        {
            sbyte ret = ocx.bCreateMultiCall("1.0", callID, callType);
            return ret != 0;
        }

        public static bool Hangup()
        {
            sbyte ret = ocx.bLeaveCall();
            return ret != 0;
        }

        public static bool SetLocalVideoHWnd(int hwnd)
        {
            sbyte ret = ocx.bSetLocalVideoHWnd("local", hwnd);
            return ret != 0;
        }

        public static bool SetRemoteVideoHWnd(int hwnd)
        {
            sbyte ret = ocx.bSetRemoteVideoHWnd("remote", hwnd);
            return ret != 0;
        }

        public static bool AcceptCall(string callID)
        {
            sbyte ret = ocx.bAcceptCall(callID);
            return ret != 0;
        }

        #endregion


        #region 音、视频设备

        public static int Volume => ocx.bGetAudioVolume();

        private static int camID = 0;

        public static int CamID => camID;


        public static event _DZwanPhoneEvents_GetVideoDeviceListResultWebEventHandler GetVideoDeviceListResultWebEvent
        {
            add { ocx.GetVideoDeviceListResultWeb += value; }
            remove { ocx.GetVideoDeviceListResultWeb -= value; }
        }

        public static event _DZwanPhoneEvents_GetAudioDeviceListResultWebEventHandler GetAudioDeviceListResultWebEvent
        {
            add { ocx.GetAudioDeviceListResultWeb += value; }
            remove { ocx.GetAudioDeviceListResultWeb -= value; }
        }

        public static string VideoProp => ocx.GetVideoProps();

        /// <summary>
        /// 设置使用摄像头
        /// </summary>
        /// <param name="index">摄像头设备索引</param>
        /// <returns></returns>
        public static bool SetCamera(int index)
        {
            sbyte ret = ocx.bSetVideoDevice(index);
            camID = index;
            return ret != 0;
        }

        /// <summary>
        /// 设置摄像头分辨率
        /// </summary>
        /// <param name="rsl">分辨率字符串</param>
        public static void SetCameraResolution(string rsl)
        {
            ocx.SetVideoProps(rsl, 1);
        }

        /// <summary>
        /// 设置码率
        /// </summary>
        /// <param name="param"></param>
        public static void SetCameraBitrate(string param, string rsl)
        {
            //ocx.SetVideoProps(param, 2);
            if (!rsl.ToLower().EndsWith("p")) rsl += "p";
            ocx.SetVideoPropsEx(rsl, param);
        }

        /// <summary>
        /// 设置音频设备
        /// </summary>
        /// <param name="render">扬声器索引</param>
        /// <param name="capture">麦克风索引</param>
        /// <returns></returns>
        public static bool SetAudioDevices(int render, int capture)
        {
            sbyte ret = ocx.SetAudioDevice(render, capture);
            return ret != 0;
        }

        /// <summary>
        /// 设置麦克风效果
        /// </summary>
        /// <param name="ec">回声抑制</param>
        /// <param name="ns">噪声消除</param>
        /// <param name="agc">自动增益</param>
        /// <returns></returns>
        public static bool SetMicrophoneParam(bool ec, bool ns, bool agc)
        {
            sbyte ret = ocx.bSetAudioParam(ec ? (sbyte)1 : (sbyte)0, ns ? (sbyte)1 : (sbyte)0, agc ? (sbyte)1 : (sbyte)0);
            return ret != 0;
        }

        public static bool SetAudioVolume(int micState, int micLevel, int spkState, int spkVolume)
        {
            sbyte ret = ocx.bSetAudioVolume(micState, micLevel, spkState, spkVolume);
            return ret != 0;
        }

        public static bool CameraOFF()
        {
            sbyte ret = ocx.bSetVideoDevice(-1);
            return ret != 0;
        }



        private static bool gettingResolutions = false;
        private static bool gettingResolutionResponed = false;
        private static string[] resolutionsResponed;
        public async static Task<string[]> GetResolutions(int id)
        {
            if (gettingResolutions) return null;

            gettingResolutions = true;

            gettingResolutionResponed = false;

            ocx.GetResolutionListResult += Ocx_GetResolutionListResult;

            ocx.GetVideoResolutions(id);

            while (!gettingResolutionResponed)
            {
                await Task.Delay(100);
            }

            gettingResolutions = false;

            return resolutionsResponed;
        }

        private static void Ocx_GetResolutionListResult(object sender, _DZwanPhoneEvents_GetResolutionListResultEvent e)
        {
            try
            {
                resolutionsResponed = e.pResolution.Split('|');

                gettingResolutionResponed = true;

                ocx.GetResolutionListResult -= Ocx_GetResolutionListResult;
            }
            catch (Exception ex)
            {
                TopVisionLog.TVLog.WriteLine("ERR OcxAPI_Ocx_GetResolutionListResult " + ex.Message);
            }
        }

        #endregion


        #region 屏幕

        public static bool SetScreenShare()
        {
            sbyte ret = ocx.bSetVideoDevice(-2);
            return ret != 0;
        }

        #endregion


        #region 拍照

        public static void Screenshots(string filename)
        {
            ocx.bScreenshots(filename);
            //return false;
        }

        #endregion


        #region SendMessage
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iType"></param>
        /// <param name="iReport"></param>
        /// <param name="pMessage"></param>
        /// <param name="pDeliverTime"></param>
        /// <param name="pReceiverURI"></param>
        /// <param name="pLocalMsgID"></param>
        /// <param name="pTMSubject"></param>
        /// <param name="iFileSeekSize"></param>
        /// <returns></returns>
        public static bool SendMessage(int iType, double iReport, string pMessage, string pDeliverTime, string pReceiverURI, string pLocalMsgID, string pTMSubject, double iFileSeekSize)
        {
            sbyte ret = ocx.bIMSSendMessage(iType, iReport, pMessage, pDeliverTime, pReceiverURI, pLocalMsgID, pTMSubject, iFileSeekSize);
            return ret != 0;
        }

        #endregion


        #region 音频

        //public bool VoiceOnly(bool active)
        //{
        //    ocx.
        //}

        #endregion

    }
}
